/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package address;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author Mavis
 */
public class PeoplePanel extends JPanel {

    JLabel namelabel = new JLabel();
    JLabel phonelabel = new JLabel();
    JLabel emaillabel = new JLabel();
    JButton delet = null;
    JButton edit = null;
    JPanel panel = new JPanel();
    People people = null;
    ArrayList contacts = null;
    ArrayList belongs = null;

    PeoplePanel(People people, ArrayList contacts, ArrayList belongs) {
        String s = null;
        this.people = people;
        this.contacts = contacts;
        this.belongs = belongs;

        panel.setLayout(null);
        panel.setOpaque(false);
        namelabel.setText(people.name);
        namelabel.setBounds(0, 0, 150, 30);

        if (people.email.size() != 0) {
            emaillabel.setText((String) people.email.get(0));
        }
        emaillabel.setBounds(160, 0, 150, 30);

        Phone phone = null;
        if (people.phone.size() != 0) {
            phone = (Phone) people.phone.get(0);
            s = phone.number;
            phonelabel.setText(s);
        }
        phonelabel.setBounds(320, 0, 150, 30);

        ImageIcon deletIcon = new ImageIcon("img/del.png");
        ImageIcon editIcon = new ImageIcon("img/edit.png");
        delet = new JButton(deletIcon);
        edit = new JButton(editIcon);
        delet.setBounds(460, 0, 30, 30);
        edit.setBounds(520, 0, 30, 30);
        delet.setContentAreaFilled(false);
        delet.setBorderPainted(false);
        edit.setContentAreaFilled(false);
        edit.setBorderPainted(false);
        delet.setOpaque(false);
        edit.setOpaque(false);

        edit.addActionListener(new editActionListener());
        delet.addActionListener(new deletActionListener());
        panel.add(namelabel);
        panel.add(emaillabel);
        panel.add(phonelabel);
        panel.add(delet);
        panel.add(edit);
    }

    private class deletActionListener implements ActionListener {

        public deletActionListener() {
        }

        public void actionPerformed(ActionEvent ae) {
            contacts.remove(people);
            BeginPanel frame = new BeginPanel(contacts, belongs);
            frame.setSize(900, 700);
            frame.setTitle("欢迎使用通讯录管理系统");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }

    private class editActionListener implements ActionListener {

        public editActionListener() {
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            CorCon frame = new CorCon(contacts, people, belongs);
            frame.setSize(500, 600);
            frame.setTitle("编辑联系人");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

        }
    }
}
